﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;

namespace MetadataBasedConfigArch.Web.Controls
{

    public class ExtBouldField : BoundField
    {
        #region Ctor

        public ExtBouldField()
            : base()
        {
            UseMethodBinding = false;
        }

        #endregion

        #region Properties

        public bool UseMethodBinding
        {
            get;
            set;
        }

        public string MethodName
        {
            get;
            set;
        }

        public string MethodParam
        {
            get;
            set;
        }

        #endregion

        #region Overrided methods

        protected override void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState)
        {
            Control child = null;
            Control control2 = null;
            bool needBinding = (!UseMethodBinding && !String.IsNullOrEmpty(DataField))
                             || (UseMethodBinding && !String.IsNullOrEmpty(MethodName));

            if ((((rowState & DataControlRowState.Edit) != DataControlRowState.Normal) && !this.ReadOnly) || ((rowState & DataControlRowState.Insert) != DataControlRowState.Normal))
            {
                TextBox box = new TextBox();
                box.ToolTip = this.HeaderText;
                child = box;
                if ((needBinding) && ((rowState & DataControlRowState.Edit) != DataControlRowState.Normal))
                {
                    control2 = box;
                }
            }
            else if (needBinding)
            {
                control2 = cell;
            }
            if (child != null)
            {
                cell.Controls.Add(child);
            }
            if ((control2 != null) && base.Visible)
            {
                control2.DataBinding += new EventHandler(this.OnDataBindField);
            }
        }

        protected override object GetValue(Control controlContainer)
        {
            if (UseMethodBinding)
            {
                if (String.IsNullOrEmpty(MethodName))
                {
                    throw new HttpException("DataItem No MethodName");
                }

                object component = null;
                if (controlContainer == null)
                {
                    throw new HttpException("DataControlField_NoContainer");
                }
                component = DataBinder.GetDataItem(controlContainer);

                if (component != null)
                {
                    MethodInfo bindingMethodInfo = component.GetType().GetMethod(MethodName);
                    if (bindingMethodInfo == null)
                    {
                        throw new HttpException(String.Format("Not Found the Method:{0}", MethodName));
                    }

                    return bindingMethodInfo.Invoke(component, new object[] { MethodParam });
                }

                return component;
            }

            return base.GetValue(controlContainer);
        }

        #endregion

    }

}